//
//  UIView+LF.m
//  AntFarms
//
//  Created by LF on 2017/2/28.
//  Copyright © 2017年 GG. All rights reserved.
//

#import "UIView+LF.h"

@implementation UIView (LF)

#pragma mark - view本身
- (void) lf_yuanjiao:(CGFloat)radius
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
}

- (void) lf_biankuang:(CGFloat) width color:(UIColor *)color
{
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
}

#pragma mark - 针对子view
- (void) lf_closeUserInteractionOfSubViews {
    self.userInteractionEnabled = NO;
    for (UIView *sbV in self.subviews) {
        sbV.userInteractionEnabled = NO;
    }
}

- (void) lf_openUserInteractionOfSubViews {
    self.userInteractionEnabled = YES;
    for (UIView *sbV in self.subviews) {
        sbV.userInteractionEnabled = YES;
    }
}

- (void) lf_setRandomColorForSbView
{
    self.backgroundColor = [UIColor randomColor];
    
    for (UIView *sbV in self.subviews) {
        sbV.backgroundColor = [UIColor randomColor];
        
        if ([sbV isKindOfClass:[UILabel class]]) {
            [(UILabel *)sbV setTextColor:[UIColor randomColor]];
        }else if ([sbV isKindOfClass:[UIButton class]]) {
            [(UIButton *)sbV setTitleColor:[UIColor randomColor] forState:UIControlStateNormal];
        }
    }
}

- (void) lf_hideAllSubViews
{
    for (UIView *view in self.subviews) {
        view.hidden = YES;
    }
}

- (void) lf_showAllSubViews
{
    for (UIView *view in self.subviews) {
        view.hidden = NO;
    }
}

// 删除
- (void) lf_removeAllSubViews
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}

/**
 *  设置部分圆角(相对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要设置的圆角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

/**
 3D动画
 */
- (void)transform3Ds {
    [self transform3Ds:nil withDuration:0];
}

/**
 3D动画

 @param animates x,y 大小变化的数值
 @param duration 持续时间
 */
- (void)transform3Ds:(NSArray *)animates withDuration:(CGFloat)duration{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.duration = duration > 0 ? duration : 0.4;

    NSArray *valueArr = animates ? animates : @[@0.1,@1.2,@0.9,@1.0];
    NSMutableArray *values = [NSMutableArray array];
    for (int i = 0; i < valueArr.count; i++) {
        CGFloat value = [[valueArr objectAtIndex:i] floatValue];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(value, value, 1.0)]];
    }
    animation.values = values;
    
    [self.layer addAnimation:animation forKey:nil];
}
@end
